import time
import pandas as pd
from sklearn.model_selection import train_test_split
from collections import Counter
from sklearn import svm, metrics
from sklearn.preprocessing import Normalizer
from sklearn.decomposition import PCA
from sklearn.naive_bayes import GaussianNB
from sklearn.tree import DecisionTreeClassifier

def load_data(dataset:str="data/dataset/dataset-20220228113900.csv"):
    t0 = time.time()
    print("Loading dataset...")
    df = pd.read_csv(dataset)
    X = df.iloc[:,:-1].values
    y = df.iloc[:,-1].values
    normalizer = Normalizer()
    normalizer.transform(X)

    pca = PCA(n_components=0.99)
    # print(X.shape)
    # X = pca.fit_transform(X)
    # print(X.shape)

    t1 = time.time()
    print("The dataset is loaded. ({} s)".format(round(t1 - t0, 2)))

    return X, y


def main():
    X, y = load_data()

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
    # Learn the digits on the train subset
    
    print("Training...")
    t0 = time.time()
    # clf = svm.SVC(gamma=0.001)
    # clf = GaussianNB()
    clf = DecisionTreeClassifier()
    clf.fit(X_train, y_train)
    t1 = time.time()
    print("Done. ({} s)".format(round(t1 - t0, 2)))

    # Predict the value of the digit on the test subset
    predicted = clf.predict(X_test)

    print(
        f"Classification report for classifier {clf}:\n"
        f"{metrics.classification_report(y_test, predicted)}\n"
    )

if __name__ == '__main__':
    main()
    # load_data()